home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / Misc / DXErr / dxerr.cpp < prev    next >
C/C++ Source or Header  |  2001-10-31  |  5KB  |  189 lines

  1. //----------------------------------------------------------------------------
  2. // File: DXErr.cpp
  3. //
  4. // Desc: The DXErr sample allows users to enter a numberical HRESULT and get back
  5. //       the string match its define.  For example, entering 0x8878000a will
  6. //       return DSERR_ALLOCATED.
  7. //
  8. // Copyright (c) 1999-2001 Microsoft Corp. All rights reserved.
  9. //-----------------------------------------------------------------------------
  10. #define STRICT
  11. #include <windows.h>
  12. #include <basetsd.h>
  13. #include <dxerr8.h>
  14. #include <tchar.h>
  15. #include "resource.h"
  16. #include "DXUtil.h"
  17.  
  18.  
  19.  
  20.  
  21. //-----------------------------------------------------------------------------
  22. // Function-prototypes
  23. //-----------------------------------------------------------------------------
  24. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam );
  25. VOID OnInitDialog( HWND hDlg );
  26. VOID LookupValue( HWND hDlg );
  27.  
  28.  
  29.  
  30.  
  31. //-----------------------------------------------------------------------------
  32. // Defines, constants, and global variables
  33. //-----------------------------------------------------------------------------
  34.  
  35.  
  36.  
  37.  
  38. //-----------------------------------------------------------------------------
  39. // Name: WinMain()
  40. // Desc: Entry point for the application.  Since we use a simple dialog for 
  41. //       user interaction we don't need to pump messages.
  42. //-----------------------------------------------------------------------------
  43. INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, 
  44.                       INT nCmdShow )
  45. {
  46.     // Display the main dialog box.
  47.     DialogBox( hInst, MAKEINTRESOURCE(IDD_MAIN), NULL, MainDlgProc );
  48.  
  49.     return TRUE;
  50. }
  51.  
  52.  
  53.  
  54.  
  55. //-----------------------------------------------------------------------------
  56. // Name: MainDlgProc()
  57. // Desc: Handles dialog messages
  58. //-----------------------------------------------------------------------------
  59. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  60. {
  61.     switch( msg ) 
  62.     {
  63.         case WM_INITDIALOG:
  64.             OnInitDialog( hDlg );
  65.             break;
  66.  
  67.         case WM_COMMAND:
  68.             switch( LOWORD(wParam) )
  69.             {
  70.                 case IDC_LOOKUP:
  71.                     LookupValue( hDlg );
  72.                     break;
  73.  
  74.                 case IDCANCEL:
  75.                     EndDialog( hDlg, IDCANCEL );
  76.                     break;
  77.  
  78.                 default:
  79.                     return FALSE; // Didn't handle message
  80.             }
  81.             break;
  82.  
  83.         case WM_DESTROY:
  84.             break; 
  85.  
  86.         default:
  87.             return FALSE; // Didn't handle message
  88.     }
  89.  
  90.     return TRUE; // Handled message
  91. }
  92.  
  93.  
  94.  
  95.  
  96. //-----------------------------------------------------------------------------
  97. // Name: OnInitDialog()
  98. // Desc: Initializes the dialogs (sets up UI controls, etc.)
  99. //-----------------------------------------------------------------------------
  100. VOID OnInitDialog( HWND hDlg )
  101. {
  102.     // Load the icon
  103. #ifdef _WIN64
  104.     HINSTANCE hInst = (HINSTANCE) GetWindowLongPtr( hDlg, GWLP_HINSTANCE );
  105. #else
  106.     HINSTANCE hInst = (HINSTANCE) GetWindowLong( hDlg, GWL_HINSTANCE );
  107. #endif
  108.     HICON hIcon = LoadIcon( hInst, MAKEINTRESOURCE( IDR_MAINFRAME ) );
  109.  
  110.     // Set the icon for this dialog.
  111.     SendMessage( hDlg, WM_SETICON, ICON_BIG,   (LPARAM) hIcon );  // Set big icon
  112.     SendMessage( hDlg, WM_SETICON, ICON_SMALL, (LPARAM) hIcon );  // Set small icon
  113.  
  114.     SendMessage( hDlg, EM_LIMITTEXT, 20, 0 );  
  115. }
  116.  
  117.  
  118.  
  119. //-----------------------------------------------------------------------------
  120. // Name: LookupValue()
  121. // Desc: 
  122. //-----------------------------------------------------------------------------
  123. VOID LookupValue( HWND hDlg )
  124. {
  125.     HRESULT hrErr = 0;
  126.     TCHAR strValue[MAX_PATH];
  127.     const TCHAR* strHRESULT;
  128.     const TCHAR* strDescription;
  129.     TCHAR strHRESULTCopy[MAX_PATH*2];
  130.     int nIndex;
  131.     int nPower = 0;
  132.     int nDigit = 0;
  133.     GetDlgItemText( hDlg, IDC_VALUE, strValue, MAX_PATH );
  134.  
  135.     nIndex = lstrlen(strValue) - 1;
  136.  
  137.     // skip whitespace
  138.     while( nIndex >= 0 )
  139.     {
  140.         if( strValue[nIndex] != ' ' && 
  141.             strValue[nIndex] != 'L' )
  142.             break;
  143.             
  144.         nIndex--;
  145.     }
  146.  
  147.     while( nIndex >= 0 )
  148.     {
  149.         // Convert to uppercase
  150.         if( strValue[nIndex] >= 'a' && strValue[nIndex] <= 'z' )
  151.             strValue[nIndex] += 'A' - 'a';
  152.  
  153.         if( strValue[nIndex] >= 'A' && strValue[nIndex] <= 'F' )
  154.             nDigit = strValue[nIndex] - 'A' + 10;
  155.         else if( strValue[nIndex] >= '0' && strValue[nIndex] <= '9' )
  156.             nDigit = strValue[nIndex] - '0';
  157.         else
  158.             break;
  159.  
  160.         hrErr += ( nDigit << (nPower*4) );
  161.  
  162.         nIndex--;
  163.         nPower++;
  164.     }
  165.  
  166.     // Use DXErr8.lib to lookup HRESULT.
  167.     strHRESULT = DXGetErrorString8( hrErr );
  168.     _tcscpy( strHRESULTCopy, TEXT("HRESULT: ") );
  169.     _tcscat( strHRESULTCopy, strHRESULT );
  170.  
  171.     strDescription = DXGetErrorDescription8( hrErr );
  172.  
  173.     TCHAR* strTemp;
  174.     while( strTemp = _tcschr( strHRESULTCopy, '&') )
  175.     {
  176.         strTemp[0] = '\r';
  177.         strTemp[1] = '\n';
  178.     }
  179.  
  180.     if( lstrlen(strDescription) > 0 )
  181.     {
  182.         _tcscat( strHRESULTCopy, TEXT("\r\nDescription: ") );
  183.         _tcscat( strHRESULTCopy, strDescription );
  184.     }
  185.  
  186.     SetDlgItemText( hDlg, IDC_MESSAGE, strHRESULTCopy );
  187.     return;
  188. }
  189.